package first.endtoend.tsm.webservices;

import first.endtoend.tsm.TSMHelper;
import first.endtoend.tsm.TSMHelperTools;
import org.apache.log4j.Logger;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.GpFunctions;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.IServiceEvents;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.elements.OperationResult;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.CheckGlobalEligibilityRequestType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.data.CheckGlobalEligibilityResponseDataType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.responses.CheckGlobalEligibilityResponseType;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by ncordina on 18/06/2015.
 * This class represent the checkGlocalEligibility webservice
 * It implements the callback and extends the TSMHelper class
 * the checkGlocalEligibility method allow to call the SOAP webservice and send the request, the response is handle by
 * the callback.
 */
public class GlobalEligibility extends TSMHelper implements IServiceEvents
{

    protected static Logger                             logger       = Logger.getLogger(TSMHelper.class.getName());
    private          CheckGlobalEligibilityResponseType responseType = new CheckGlobalEligibilityResponseType();
    private          HashMap<String, String>            display      = new HashMap<String, String>();


    /**
     * Class Constructor
     * Take the URL of SPTSM Server in parameter and instanciate the CallBack and the controller of Wolf library
     *
     * @param URL
     */
    public GlobalEligibility(String URL)
    {
        super(URL);
    }

    @Override
    public void Starting()
    {
        System.out.println("_____________________________________________\nSTART GLOBAL ELIGIBILITY");
        logger.info("CallBackStarting CheckGlobalEligibility");
    }

    @Override
    /**
     * This Method is synchronized, it manage the response from the TSM Server
     *
     */
    public synchronized void Completed(OperationResult operationResult)
    {
        System.out.println("______________________________________________\nCOMPLETED");
        logger.info(operationResult);
        System.out.println("#####METHOD = " + operationResult.MethodName + "\n____________\n");
        //   System.out.println(operationResult.Result.toString());
        this.checkGlobalEligibilityResponse(operationResult);
        notify();
    }


    /**
     * Call checkGlobalEligibility Webservice from SPTSM
     * This function create all mandatory parameters for the SOAP webservice
     */
    public synchronized HashMap<String, String> checkGlobalEligibility() throws InterruptedException
    {

        CheckGlobalEligibilityRequestType requestType = new CheckGlobalEligibilityRequestType(); //Request creation

        requestType.FunctionCallIdentifier = GpFunctions.CheckGlobalEligibility.getFunction();
        requestType.ValidityPeriod = validityPeriod;
        requestType.Service = tools.initService();
        requestType.MobileSubscription = tools.initMobileSubcription();
        requestType.SecureElement = tools.initSecureElement();
        requestType.ServiceQualifier = tools.initServiceQualifier();

        tsmController.checkGlobalEligibility(this, requestType); //Function Call
        wait();
        System.out.println("_______HAHSPMAP DISPLAY_________");
        TSMHelperTools.hashMapDisplay(display);

        return display;

    }

    /**
     * This function is a test function, no SPTSM call is done
     * This function create all mandatory parameters for the SOAP webservice
     */
    public synchronized HashMap<String, String> checkGlobalEligibilityTest() throws InterruptedException
    {

        CheckGlobalEligibilityRequestType requestType = new CheckGlobalEligibilityRequestType(); //Request creation

        requestType.FunctionCallIdentifier = GpFunctions.CheckGlobalEligibility.getFunction();
        requestType.ValidityPeriod = validityPeriod;
        requestType.Service = tools.initService();
        requestType.MobileSubscription = tools.initMobileSubcription();
        requestType.SecureElement = tools.initSecureElement();
        requestType.ServiceQualifier = tools.initServiceQualifier();

        display = TSMHelperTools.FillTestMatrix();

        return display;

    }

    public synchronized void checkGlobalEligibilityResponse(OperationResult operationResult)
    {
        display = TSMHelperTools.handleResponse(operationResult, GpFunctions.CheckGlobalEligibility.getFunction());
        CheckGlobalEligibilityResponseType     responseType     = (CheckGlobalEligibilityResponseType) operationResult.Result;
        CheckGlobalEligibilityResponseDataType responseDataType = responseType.ResponseData;
        display.put("Eligible", responseDataType.Eligible.toString());
    }

    public synchronized HashMap<String, String> getDisplay()
    {
        return display;
    }
/*
    private SecurityObject setSecurityObject()
    {
        ArrayList<String> ids = new ArrayList<String>();
        ids.add("SIG-444F9FD28BD702C401143764394401532");
        ids.add("SIG-444F9FD28BD702C401143764394400728");

        String issueName    = "CN=gto_ca,OU=first.endtoend.tsm,O=gemalto,L=sophia,ST=paca,C=fr";
        String serialNumber = "674617944596472";
        String token =
                "MIIDDDCCAnWgAwIBAgIHAmWPuvJf" +
                        "+DANBgkqhkiG9w0BAQUFADBeMQswCQYDVQQGEwJmcjENMAsGA1UECBMEcGFjYTEPMA0GA1UEBxMGc29waGlhMRAwDgYDVQQKEwdnZW1hbHRvMQwwCgYDVQQLEwN0c20xDzANBgNVBAMMBmd0b19jYTAeFw0xMjA0MTEwODE5MjZaFw0yMjA0MTEwODE5MjZaMIGYMRkwFwYDVQQDDBB0ZXN0ZXI6QkFOSyBURVNUMQswCQYDVQQGEwJGUjENMAsGA1UECAwEUEFDQTEZMBcGA1UEBwwQU29waGlhIEFudGlwb2xpczEQMA4GA1UECgwHR2VtYWx0bzEQMA4GA1UECwwHVFNNX2dlbTEgMB4GCSqGSIb3DQEJARYRYWRtaW5AZ2VtYWx0by5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMSRWppQt8KWKjbvU7OVE5YTaOJS2qJnN3boFrDADljk1Tv9DEjnZnlQYSBRLuaxuOv3jEzzVqOMFnzdfROC30cVBOLx9J0V1OrAP1NB/zJgPk3qF3R2KR6S1NprVzSji3/O5yOmef2Ytce8OBxpQzSqjjIuigSHckDE3GEMSM2RAgMBAAGjgZgwgZUwHQYDVR0OBBYEFNI4gqT/3pP8Tv2MeLZvbAkZYQK2MB8GA1UdIwQYMBaAFLeVMo1XHS0xMsXQzX4HIqFZg9U4MAwGA1UdEwEB/wQCMAAwRQYDVR0fBD4wPDA6oDigNoY0aHR0cDovLzEwLjUwLjY0LjE0OjcwMDEvVXNlck1uZ3RDbGllbnRTZXJ2bGV0L0dldENybDANBgkqhkiG9w0BAQUFAAOBgQBNdJ/o9DXSyrwdkkNjiNNnm8xxrtuiNeWO++k5qTfqAi8uEZNZxCXZplRZCb97oOVOFVBipm/8SP/yFEKCr+guBRY0T4lYnYKaP2EKMjuMRey7OpCR6DenlFOLkqETTC4nNMNumC2iFE/mhMjclDBG6OjmLetvDq5R3sW9Oasb7w==";
        String URLAction  = "http://globalplatform.org/globaleligibilityinfo/GlobalEligibilityInfo/CheckGlobalEligibilityRequest";
        String URLFrom    = "http://banktest.com/?sender=2B0601040181F803";
        String MessageID  = "uuid:c77e57f0-7a8b-451b-aa47-390db8b175d1";
        String URLTo      = "http://vgerndvud1098:7101/Tsm/GlobalEligibilityInfo";
        String pathToCert = Certs.TESTER_KEYSTORE.getPathToCert();
        String keyAlias   = Certs.TESTER_KEYSTORE.getKeyAlias();
        char[] password   = Certs.TESTER_KEYSTORE.getPassword();

        return new SecurityObject(ids, serialNumber, issueName, token, URLAction, URLFrom, MessageID, URLTo, pathToCert, keyAlias, password);

    }
*/
}
